/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Polygon;
import physics.Physics;

/**
 *
 * @author wida52629504
 */
public class Enemy extends GameCharacter {

    private int lvl;
    private Polygon enPoly;
    private int distanciaSeguimiento = 150;
    private int distanciaDisparo = 300;
    private int distanciaParada = 30;
    private int contadorCC = 0;
    private int contadorDist = 0;

    public Enemy() {
        super();
    }

    public Enemy(ResultSet iniValues, ResultSet animations) {
        setAnimation(new Animation());
        setAnimationR(new Animation());
        setAnimationL(new Animation());
        setAnimationJumpR(new Animation());
        setAnimationJumpL(new Animation());
        setAnimationRR(new Animation());
        setAnimationRL(new Animation());
        setAnimationAtR(new Animation());
        setAnimationAtL(new Animation());
        setAnimationShtR(new Animation());
        setAnimationShtL(new Animation());
        try {

            while (iniValues.next()) {
                setName(iniValues.getString(1));
                setMaxLife(iniValues.getInt(2));
                setLife(getMaxLife());
                setSpeed(iniValues.getFloat(3));
                setxLeft(iniValues.getInt(4));
                setxRight(iniValues.getInt(5));
                setyUp(iniValues.getInt(6));
                setyBottom(iniValues.getInt(7));
                setPoly(getxLeft(), getxRight(), getyUp(), getyBottom());
                setAxLeft(iniValues.getInt(8));
                setAxRight(iniValues.getInt(9));
                setAyUp(iniValues.getInt(10));
                setAyBottom(iniValues.getInt(11));
                setAtackPoly(getAxLeft(), getAxRight(), getAyUp(), getAyBottom());
                this.maxAtackTime = iniValues.getInt(12);
                this.maxShootTime = iniValues.getInt(13);
                setMargenUp(iniValues.getInt(14));
                setMargenDown(iniValues.getInt(15));
                distanciaSeguimiento = iniValues.getInt(16);
                distanciaDisparo = iniValues.getInt(17);
                distanciaParada = iniValues.getInt(18);
                setExp(iniValues.getInt(19));
                setLeft(false);
                this.atackTime = 0;
                this.shootTime = 0;
                setSaltable(true);
                setAtack(false);
                setShoot(false);
                setDead(false);
            }
            try {
                while (animations.next()) {
                    String accion = animations.getString("accion");
                    Image img = new Image(animations.getString("img_path"));
                    int frameTime = animations.getInt("frameTime");

                    if (accion.equals("jumpL")) {
                        getAnimationJumpL().addFrame(img, frameTime);
                    } else if (accion.equals("jumpR")) {
                        getAnimationJumpR().addFrame(img, frameTime);
                    } else if (accion.equals("standR")) {
                        getAnimationR().addFrame(img, frameTime);
                    } else if (accion.equals("standL")) {
                        getAnimationL().addFrame(img, frameTime);
                    } else if (accion.equals("moveR")) {
                        getAnimationRR().addFrame(img, frameTime);
                    } else if (accion.equals("moveL")) {
                        getAnimationRL().addFrame(img, frameTime);
                    } else if (accion.equals("cxcR")) {
                        getAnimationAtR().addFrame(img, frameTime);
                    } else if (accion.equals("cxcL")) {
                        getAnimationAtL().addFrame(img, frameTime);
                    } else if (accion.equals("shL")) {
                        getAnimationShtL().addFrame(img, frameTime);
                    } else if (accion.equals("shR")) {
                        getAnimationShtR().addFrame(img, frameTime);
                    } else if (accion.equals("dieR")) {
                        getAnimationDieR().addFrame(img, frameTime);
                        getAnimationDieR().setLooping(false);
                    } else if (accion.equals("dieL")) {
                        getAnimationDieL().addFrame(img, frameTime);
                        getAnimationDieL().setLooping(false);
                    } else if (accion.equals("hurtR")) {
                        getAnimationHurtR().addFrame(img, frameTime);
                    } else if (accion.equals("hurtL")) {
                        getAnimationHurtL().addFrame(img, frameTime);
                    }

                    setAnimation(getAnimationR());
                }

            } catch (SlickException e) {
                System.out.println(e);


            }
        } catch (SQLException se) {
            System.out.println(se);
        }
    }

    public int getDistanciaParada() {
        return distanciaParada;
    }

    public void setDistanciaParada(int distanciaParada) {
        this.distanciaParada = distanciaParada;
    }

    public int getDistanciaSeguimiento() {
        return distanciaSeguimiento;
    }

    public void setDistanciaSeguimiento(int distanciaSeguimiento) {
        this.distanciaSeguimiento = distanciaSeguimiento;
    }

    public void Update(GameContainer gc, int delta, Physics phys, PlayableChar player, ArrayList<Bullet> b) {
            if (!isDead()) {
                if (!isHurt()) {
                contadorCC = contadorCC + delta;
                contadorDist = contadorDist + delta;


                if (player.getPosX() < getPosX()) {
                    setLeft(true);
//            if (getPosX() - player.getPosX() < distanciaDisparo) {
//             setLeft(true);   if (getPosX() - player.getPosX() > distanciaParada) {
//
//                }
//            }
                    if (getPosX() - player.getPosX() < getDistanciaDisparo()) {
                        if (getPosX() - player.getPosX() > getDistanciaSeguimiento()) {
                            if (getContadorDist() > 1500) {
                                setContadorDist(0);
                                if (player.getPosY() - getPosY() < 100 && player.getPosY() - getPosY() > -100) {
                                    setAtack(false);
                                    setShoot(true);
                                    b.add(new Bullet(getPosX(), getPosY() + 36, false, isLeft()));
                                }
                            }
                        }
                    }


                    if (getPosX() - player.getPosX() < distanciaSeguimiento) {
                        if (getPosX() - player.getPosX() > distanciaParada) {
                            if (player.getPosY() - getPosY() < 100 && player.getPosY() - getPosY() > -100) {
                                setVelX(-1);
                            }


                        } else {
                            setVelX(0);


                            if (contadorCC > 1500) {
                                contadorCC = 0;
                                if (player.getPosY() - getPosY() < 100 && player.getPosY() - getPosY() > -100) {
                                    setAtack(true);
                                    setShoot(false);


                                    try {
                                        if (phys.entityToEntityCollision(this.getAtackPoly(), player.getPoly())) {
                                            player.setLife(player.getLife() - 10);
                                            player.setHurt(true);
                                        }
                                    } catch (Exception e) {
                                        System.out.println(e);


                                    }
                                }
                            }
                        }

                    }

                } else {
                    setLeft(false);
                    if (player.getPosX() - getPosX() < getDistanciaDisparo()) {
                        if (player.getPosX() - getPosX() > getDistanciaSeguimiento()) {
                            if (getContadorDist() > 1500) {
                                setContadorDist(0);
                                if (player.getPosY() - getPosY() < 100 && player.getPosY() - getPosY() > -100) {
                                    setAtack(false);
                                    setShoot(true);
                                    b.add(new Bullet(getPosX(), getPosY() + 36, false, isLeft()));
                                }
                            }
                        }
                    }
                    if (player.getPosX() - getPosX() < distanciaSeguimiento) {
                        if (player.getPosX() - getPosX() > distanciaParada) {
                            if (player.getPosY() - getPosY() < 100 && player.getPosY() - getPosY() > -100) {
                                setVelX(1);
                            }


                        } else {
                            setVelX(0);


                            if (contadorCC > 1500) {
                                contadorCC = 0;
                                if (player.getPosY() - getPosY() < 100 && player.getPosY() - getPosY() > -100) {
                                    setAtack(true);
                                    setShoot(false);
                                    try {
                                        if (phys.entityToEntityCollision(this.getAtackPoly(), player.getPoly())) {
                                            player.setLife(player.getLife() - 10);
                                            player.setHurt(true);
                                        }
                                    } catch (Exception e) {
                                        System.out.println(e);


                                    }
                                }
                            }
                        }
                    }
                }
            }

            super.Update(gc, delta, phys);
            setVelX(0);
        }
    }

    @Override
    public void Render(GameContainer gc, Graphics g, int incX, int incY) {
        if (!isDead()) {
            super.Render(gc, g, incX, incY);
        }
    }

    public Polygon getEnPoly() {
        return enPoly;




    }

    public void setEnPoly(Polygon enPoly) {
        this.enPoly = enPoly;




    }

    public int getContadorCC() {
        return contadorCC;


    }

    public void setContadorCC(int contadorCC) {
        this.contadorCC = contadorCC;


    }

    public int getContadorDist() {
        return contadorDist;


    }

    public void setContadorDist(int contadorDist) {
        this.contadorDist = contadorDist;


    }

    public int getLvl() {
        return lvl;


    }

    public void setLvl(int lvl) {
        this.lvl = lvl;


    }

    public int getDistanciaDisparo() {
        return distanciaDisparo;






    }

    public void setDistanciaDisparo(int distanciaDisparo) {
        this.distanciaDisparo = distanciaDisparo;



    }
}
